package com.dmooo.yxmmerchant.view.presenter;

import android.content.Context;

import com.common.mvpbase.BasePresenter;
import com.common.net.listener.HttpOnNextListener;
import com.dmooo.yxmmerchant.domain.MerchantInfoBean;
import com.dmooo.yxmmerchant.view.contract.MainContract;
import com.dmooo.yxmmerchant.view.model.MainModel;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;

public class MainPresenter extends BasePresenter<MainContract.MainView> implements MainContract.MainPtr {
    private MainModel model;
    private GsonBuilder builder=new GsonBuilder();
    public MainPresenter(MainContract.MainView mView, Context context) {
        super(mView);
        model=new MainModel(context);
        builder.serializeNulls();
    }

    @Override
    public void getMerchantInfo(String token) {
        model.getMerchantInfo(token, new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                try {
                    mView.showMerchantInfoBean(new Gson().fromJson(new JSONObject(builder.create().toJson(o)).toString(),MerchantInfoBean.class));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void editMerchantInfoBean(String token,MerchantInfoBean merchantInfoBean) {
        model.editMerchantInfoBean(token, merchantInfoBean, new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                mView.editMerchantInfoBeanSuccess();
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void editMerchantAvatar(String token, File file) {
        model.editMerchantAvatar(token, file, new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                mView.editMerchantAvatarSuccess();
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }
}
